Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

[ 前端工具 ] - jQuery

[ 前端工具 ] - jQuery

[看懂 IT 術語] AI 人工智慧

[看懂 IT 術語] AI 人工智慧

來寫測試吧!

來寫測試吧!






留言討論